<?PHP
require_once('ConnectionMysql.php');
require_once('Categoria.php');

class CategoriaDAO
{
    private $connection = NULL;


    /*************************************************************************
    * Name: __construct
    * Description: Construtor da Classe. Inicializar os Atributos.
    * Parameters: 
    * Returns: Nenhum
    * Author: Felipe O. Simões
    * Last Modified: 07/04/2012 - Felipe O. Simões
    *************************************************************************/
    public function __construct()
    {
        $this->connection = new ConnectionMysql(); // Cria a Conexão.
    }


    /*************************************************************************
    * Name: 
    * Description: 
    * Parameters: 
    * Returns: 
    * Author: Felipe O. Simões
    * Last Modified: 07/04/2012 - Felipe O. Simões
    *************************************************************************/
    public function insert(Categoria $obj)
    {
        $sql = "INSERT INTO categoria (categoria, isAtivo) VALUES ('". $obj->getCategoria() ."', '". $obj->getIsAtivo() ."')";
        return $this->connection->queryConnect($sql);
    }


    /*************************************************************************
    * Name: 
    * Description: 
    * Parameters: 
    * Returns: 
    * Author: Felipe O. Simões
    * Last Modified: 07/04/2012 - Felipe O. Simões
    *************************************************************************/
    public function deletePorId($id)
    {
        $sql = "UPDATE categoria SET isAtivo='N'WHERE idCategoria=". $id;
        $this->connection->queryConnect($sql);
    }


    /*************************************************************************
    * Name: 
    * Description: 
    * Parameters: 
    * Returns: 
    * Author: Felipe O. Simões
    * Last Modified: 07/04/2012 - Felipe O. Simões
    *************************************************************************/
    public function deletePorObj(Categoria $obj)
    {
        $sql = "UPDATE categoria SET isAtivo='N' WHERE idCategoria=". $obj->getIdCategoria();
        $this->connection->queryConnect($sql);
    }


    /*************************************************************************
    * Name: 
    * Description: 
    * Parameters: 
    * Returns: 
    * Author: Felipe O. Simões
    * Last Modified: 07/04/2012 - Felipe O. Simões
    *************************************************************************/
    public function update(Categoria $obj)
    {
        $sql = "UPDATE categoria SET categoria='". $obj->getCategoria() ."', isAtivo='". $obj->getIsAtivo() ."'WHERE idCategoria=". $obj->getIdCategoria();
        $this->connection->queryConnect($sql);
    }


    /*************************************************************************
    * Name: 
    * Description: 
    * Parameters: 
    * Returns: 
    * Author: Felipe O. Simões
    * Last Modified: 07/04/2012 - Felipe O. Simões
    *************************************************************************/
    public function select()
    {
        $sql = "SELECT idCategoria, categoria FROM categoria WHERE isAtivo='S'";
     
        $result =  $this->connection->queryConnect($sql);
        
        if (mysql_num_rows($result) > 0)
        {
            $array = array();
            for ($i = 0; $i < mysql_num_rows($result); $i++)
            {
                $array[$i] = new Categoria();
                $array[$i]->setIdCategoria(mysql_result($result, $i, "idCategoria"));
                $array[$i]->setCategoria(mysql_result($result, $i, "categoria"));
            }           

            return $array;
        }
        else
        {
                return array();
        }               
    }
}
?>